package utilities;

import model.ProgramGraph;
import model.TransitionSystem;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

/** Class representing the elements in one XML file
 * @author DarkoLaptop2
 *
 */
/**
 * @author DarkoLaptop2
 *
 */
public class Elements {
	
	/**
	 * This method is a constructor of the class
	 * @param doc - the document
	 */
	public Elements(Document doc, String stOrTr) {
		super();
		this.doc = doc;
		this.stOrTr = stOrTr;
	}

	private Document doc;
	private NodeList type;
	private NodeList nList;
	private String stOrTr;
	
	/**
	 *  Method for getting the root element of the XML file
	 */
	public void getRootElement()
	{
		System.out.println(doc.getDocumentElement().getNodeName());
		type = doc.getElementsByTagName("type");
    	String typeOfModel = type.item(0).getTextContent();
	
    	if (typeOfModel.equals("fa")) {
    		System.out.println("Type : Finite Automation");
    	} else {
    		if (typeOfModel.equals("pda"))
    		{
    			System.out.println("Type : PushDown Automation");
    		}
    	}
	}
	//dve  funkcii koi vrakjaat graph
	
	/** Method which creates a graph without variables for a given Document
	 * @param document - the document from the XML file
	 * @return a new graph without variables
	 */
	TransitionSystem generateGraphWithoutVariables(Document document){
		
		TransitionSystem graph=new TransitionSystem();
		nList = doc.getElementsByTagName(stOrTr);
//		System.out.println("----------------------------");
		
		for (int temp = 0; temp < nList.getLength(); temp++) {
			 
    		Node nNode = nList.item(temp);
 
//    		System.out.println("\nCurrent Element :" + nNode.getNodeName());
 
    		if (nNode.getNodeType() == Node.ELEMENT_NODE && stOrTr.equals("state")) {
    			Element eElement = (Element) nNode;
    			int index=Integer.parseInt(eElement.getAttribute("id"));
    			graph.addNode(index, eElement.getAttribute("name"));
 
//    			System.out.println("State id : " + eElement.getAttribute("id"));
//    			System.out.println("State name : " + eElement.getAttribute("name"));
    			
    			
    			if (eElement.getElementsByTagName("initial").item(0) != null)
    			{
    				graph.setInitialNode(index);
//    				System.out.println("Initial state");
    			}
    			if (eElement.getElementsByTagName("label").item(0) != null)
    			{
    				graph.setBadNode(index);
//    				System.out.println("Bad state");
    			}
    			//System.out.println("From : " + eElement.getElementsByTagName("from").item(0).getTextContent());
    			//System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());
    			//System.out.println("Read : " + eElement.getElementsByTagName("read").item(0).getTextContent());
				} else {
					Element eElement = (Element) nNode;
					int from= Integer.parseInt(eElement.getElementsByTagName("from").item(0).getTextContent());
					int to=Integer.parseInt(eElement.getElementsByTagName("to").item(0).getTextContent());
					graph.addEdge(from,  eElement.getElementsByTagName("read").item(0).getTextContent(), to);
//					System.out.println("From : " + eElement.getElementsByTagName("from").item(0).getTextContent());
//	    			System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());
//	    			System.out.println("Read : " + eElement.getElementsByTagName("read").item(0).getTextContent());
					
				}
    	}
		return graph;
		
	}
	
	/** Method for printing the document
	 * 
	 */
	public void print ()
	{
		TransitionSystem graph=new TransitionSystem();
		nList = doc.getElementsByTagName(stOrTr);
		System.out.println("----------------------------");
		
		for (int temp = 0; temp < nList.getLength(); temp++) {
			 
    		Node nNode = nList.item(temp);
 
    		System.out.println("\nCurrent Element :" + nNode.getNodeName());
 
    		if (nNode.getNodeType() == Node.ELEMENT_NODE && stOrTr.equals("state")) {
    			Element eElement = (Element) nNode;
    			int index=Integer.parseInt(eElement.getAttribute("id"));
    			graph.addNode(index, eElement.getAttribute("name"));
 
    			System.out.println("State id : " + eElement.getAttribute("id"));
    			System.out.println("State name : " + eElement.getAttribute("name"));
    			
    			
    			if (eElement.getElementsByTagName("initial").item(0) != null)
    	
    				System.out.println("Initial state");
    			
    			if (eElement.getElementsByTagName("label").item(0) != null)
    		    	
    				System.out.println("Bad state");
    			
    			//System.out.println("From : " + eElement.getElementsByTagName("from").item(0).getTextContent());
    			//System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());
    			//System.out.println("Read : " + eElement.getElementsByTagName("read").item(0).getTextContent());
				} else {
					Element eElement = (Element) nNode;
					System.out.println("From : " + eElement.getElementsByTagName("from").item(0).getTextContent());
	    			System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());
	    			System.out.println("Read : " + eElement.getElementsByTagName("read").item(0).getTextContent());
					
				}
    	}
		
	}
}
